module how {
    export class CardGroup extends eui.Group {
        public card: how.CardGroup;
        public offsexY: number = 30;
        public static PUT: string = "put";
        private _selected: boolean = false;
        private _dragGroup: eui.Group;
        public get icon(): eui.Image {
            return this.numChildren > 1 ? this.getChildAt(1) as eui.Image : null;
        }
        public get bg(): eui.Image {
            return this.numChildren > 1 ? this.getChildAt(0) as eui.Image : null;
        }
        public get source(): string | egret.Texture {
            return this.icon ? this.icon.source : null;
        }
        public set source(value: string | egret.Texture) {
            if (this.icon) {
                this.icon.source = value;
            }
        }
        public get dragGroup(): eui.Group {
            return this._dragGroup;
        }
        public get selected(): boolean {
            return this._selected;
        }
        public set selected(value: boolean) {
            var isChange = this._selected != value;
            this._selected = value;
            if (isChange) {
                this.y = this.selected ? this.y - this.offsexY : this.y + this.offsexY;
                if (value) {
                    this.stage.dispatchEventWith("onCardSelected", false, this);
                }
            }
        }
        private _hight: boolean = true;
        public set hight(value: boolean) {
            this.selected = false;
            this.alpha = value ? 1 : 0.5;
            this._hight = value;
            this.touchEnabled = value;
            this.touchChildren = value;
        }
        public get hight(): boolean {
            return this._hight;
        }
        public constructor() {
            super();
            this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTouchTap, this);
            this.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onTouchBegin, this);
        }
        protected onCardSelected(event: egret.Event): void {
            if (event.data != this) {
                this.selected = false;
            }
        }
        protected onTouchTap(): void {
            this.selected = !this.selected;
            if (!this.selected) {
                this.dispatchEventWith(CardGroup.PUT);
            }
        }
        protected onTouchBegin(): void {
            this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.onStageTouchMove, this);
            this.stage.addEventListener(egret.TouchEvent.TOUCH_END, this.onStageTouchEnd, this);
        }
        protected onStageTouchEnd(event: egret.TouchEvent): void {
            this.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE, this.onStageTouchMove, this);
            this.stage.removeEventListener(egret.TouchEvent.TOUCH_END, this.onStageTouchEnd, this);
            if (this.dragGroup) {
                this.dragGroup.parent.removeChild(this.dragGroup);
                this._dragGroup = null;
                this.alpha = 1;
                var bounds = this.getTransformedBounds(this.stage);
                var rect = new egret.Rectangle(0, this.selected ? bounds.y + this.offsexY : bounds.y, this.stage.stageWidth, bounds.height);
                if (!rect.contains(event.stageX, event.stageY)) {
                    this.selected = false;
                    this.dispatchEventWith(CardGroup.PUT);
                }
            }
        }
        protected onStageTouchMove(event: egret.TouchEvent): void {
            if (!this.dragGroup) {//如果没有拖拽的图片
                this._dragGroup = new eui.Group();
                for (let i = 0; i < this.$children.length; i++) {
                    var image = new eui.Image()
                    image.texture = (this.$children[i] as eui.Image).texture;
                    if (i > 0) {
                        image.verticalCenter = 12;
                        image.horizontalCenter = 0;
                    }
                    this.dragGroup.addChild(image);
                }
                this.stage.addChild(this.dragGroup);
                this.alpha = 0.5;
            }
            this.dragGroup.x = event.stageX - this.dragGroup.width / 2;
            this.dragGroup.y = event.stageY - this.dragGroup.height / 2;
        }
        public $onAddToStage(stage: egret.Stage, nestLevel: number): void {
            super.$onAddToStage(stage, nestLevel);
            this.stage.addEventListener("onCardSelected", this.onCardSelected, this);
        }
        public $onRemoveFromStage(): void {
            super.$onRemoveFromStage();
            this.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE, this.onStageTouchMove, this);
            this.stage.removeEventListener(egret.TouchEvent.TOUCH_END, this.onStageTouchEnd, this);
            this.stage.removeEventListener("onCardSelected", this.onCardSelected, this);
        }
    }
}